package be.vdab.filters;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;


/**
 * Servlet Filter implementation class JPAFilter
 */
@WebFilter("*.htm")
public class JPAFilter implements Filter {
private static EntityManagerFactory entityManagerFactory;
private static ThreadLocal<EntityManager> entityManagers;

    public JPAFilter() {
        // TODO Auto-generated constructor stub
    }

	
	public void destroy() {
		entityManagerFactory.close();
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		entityManagers.set(entityManagerFactory.createEntityManager());
		try{
			chain.doFilter(request, response);
	} finally{
		EntityManager entityManager = entityManagers.get();
		if(entityManager.getTransaction().isActive()){
			entityManager.getTransaction().rollback();
		} entityManager.close();
		entityManagers.remove();
	} 
		}

	public void init(FilterConfig fConfig) throws ServletException {
		entityManagerFactory = Persistence.createEntityManagerFactory("cultuurhuis");
		entityManagers = new ThreadLocal<EntityManager>();
	}
	
	public static EntityManager getEntityManager(){
		return entityManagers.get();
	}

}
